package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加購物車
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = ShoppingCart.builder().build();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        
        shoppingCart.setUserId(BaseContext.getCurrentId());
        
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getList(shoppingCart); // 通過查找數據庫獲得購物車信息

//        判斷當前商品是否在購物車
        if (shoppingCartList != null && shoppingCartList.size() >= 1) {
             shoppingCart = shoppingCartList.get(0);
             shoppingCart.setNumber(shoppingCart.getNumber() + 1);

             shoppingCartMapper.update(shoppingCart); // 更新購物車數據
        } else {
            // 判斷加入購物車中的是菜品還是套倉

            Long dishId = shoppingCartDTO.getDishId(); // 獲得參數中的菜品id屬性

//            判斷其是否爲空
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看購物車
     * @return
     */
    @Override
    public List<ShoppingCart> select() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getList(ShoppingCart.builder(). userId(BaseContext.getCurrentId()).build());
        return shoppingCartList;
    }

    /**
     * 刪除購物車中單個商品
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
            Long setmealId = shoppingCartDTO.getSetmealId();
            Long dishId = shoppingCartDTO.getDishId();

//            判斷要刪除的商品是菜品還是套倉
        if (dishId != null) {
//            判斷為菜品,根據菜品ID查出相對的商品
            ShoppingCart shoppingCart = shoppingCartMapper.getBydishId(dishId);

            if (shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.update(shoppingCart);
            } else {
                shoppingCartMapper.deleteBydishId(dishId);
            }

        } else {
            ShoppingCart shoppingCart = shoppingCartMapper.getBysetmealId(setmealId);

            if (shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.update(shoppingCart);
            } else {
                shoppingCartMapper.deleteBysetmealId(setmealId);
            }

        }







    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
